package com.boco.pciv.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.entity.sys.DeptInfo;
import com.boco.pciv.service.sys.IDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value="/sys/dept")
public class DeptController extends PcivController {
    Logger logger = LoggerFactory.getLogger(DeptController.class);
	@Resource
	private IDeptService svr;

    @GetMapping("/list")
    public PageResultBase getDeptList(){
        List<DeptInfo> list = svr.getAll();
        //addOperationLog("查询","部门信息查询");
        LayUIPageResult rtn = new LayUIPageResult();
        rtn.setCode(0);
        rtn.setData(list);
        rtn.setCount(new Long(list.size()));
        rtn.setPage(1);
        rtn.setLimit(list.size());
        return rtn;
    }

    @GetMapping("/listWithRoot")
    public BizResponse getDeptListWithRoot(){
        List<DeptInfo> list = svr.getAll();
        //添加根节点
        DeptInfo root = new DeptInfo();
        root.setId(Constants.DeptParentID);
        root.setName(Constants.DeptParentName);
        root.setLevelId(0);
        list.add(root);
        return BizResponseUtil.list(list);
    }

    @GetMapping("/deptTree")
    public BizResponse getDeptTree(String parentId){
       List<BizTree> trees = new ArrayList<BizTree>();
        BizTree tree = svr.getDeptTreeByParent(parentId);
        trees.add(tree);
        return BizResponseUtil.list(trees);
    }

    @GetMapping("/deptTree4Select")
    public BizResponse getDeptTree4Select(String parentId){
        List<SelectItem> items = svr.getDeptTreeByParent4Select(parentId);
        return BizResponseUtil.list(items);
    }

    @GetMapping("/info")
    public BizResponse getDeptInfo(String id){
        DeptInfo detail = svr.getOne(id);
        return  BizResponseUtil.single(detail);
    }

    @PostMapping("/add")
    public BizResponse addDept(@Valid DeptInfo res, BindingResult result){
        if(res ==null){
            return BizResponseUtil.fail("新增信息不能为空");
        }
        if (result.hasErrors()){
            return BizResponseUtil.validFail(result);
        }

//        //是否存在id
//        DeptInfo info = svr.getOne(res.getId());
//        if(info!= null){
//            return BizResponseUtil.fail("该id已经存在");
//        }
        try{
            res.setCreateUser(getCurUserId());
            svr.add(res);
            addOperationLog("新增",String.format("新增部门:%s",res.getName()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @PostMapping("/delete")
    public BizResponse deleteDept(String id){
        try{
            svr.delete(id);
            addOperationLog("删除",String.format("删除部门:%s",id));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @PostMapping("/edit")
    public BizResponse editDept(@Valid DeptInfo dept, BindingResult result){
        if(dept ==null){
            return BizResponseUtil.fail("信息不能为空");
        }
        if (result.hasErrors()){
            return BizResponseUtil.validFail(result);
        }
        try{
            dept.setUpdateUser(getCurUserId());
            svr.edit(dept);
            addOperationLog("编辑",String.format("编辑部门:%s",dept.getId()));
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (PcivException ex){
            logger.warn(MSG_ExeERROR, ex);
            return BizResponseUtil.error(ex);
        } catch(Exception e){
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR+e.getMessage());
        }
    }

    @GetMapping("/checkDeptIdValidate")
    public String checkDeptIdValidate(String id){
        if (StrUtil.isBlank(id)){
            return "false";
        }
        DeptInfo info = svr.getOne(id);
        if(info!=null){
            return "false";
        }
        else
            return "true";
    }
}
